﻿#pragma once
#include "LuaObjMacro.h"

#define USE_DL_MALLOC 1

#if USE_NED_MALLOC

#include "malloc/nedmalloc.h"
typedef nedpool* mempoll_t;
#define mempollcreate nedcreatepool
#define mempolldestroy neddestroypool
#define mempollmalloc nedpmalloc
#define mempollrealloc nedprealloc
#define mempollfree nedpfree
#define mempoll_calloc nedpcalloc
#define USE_MEM_POLL 1

#elif USE_DL_MALLOC

#include "malloc/dlmalloc.h"
typedef mspace mempoll_t;
#define mempollcreate create_mspace
#define mempolldestroy destroy_mspace
#define mempollmalloc mspace_malloc
#define mempollrealloc mspace_realloc
#define mempollfree mspace_free
#define mempoll_calloc mspace_calloc
#define USE_MEM_POLL 1

#endif

#undef malloc
#undef free
#undef realloc
#undef calloc

#if USE_MEM_POLL


class LUAOBJ_API LuaMemPool
{
public:
	LuaMemPool(){
		m_pool = mempollcreate(0, 0);
	}

	~LuaMemPool(){
		mempolldestroy(m_pool);
	}

	inline void* malloc(size_t bytes){
		return mempollmalloc(m_pool, bytes);
	}

	inline void free(void* mem){
		mempollfree(m_pool, mem);
	}

	inline void* realloc(void* mem, size_t newsize){
		return mempollrealloc(m_pool, mem, newsize);
	}

	inline void* calloc(size_t n_elements, size_t elem_size){
		return mempoll_calloc(m_pool, n_elements, elem_size);
	}
private:
	mempoll_t m_pool;
};


#else

class LUAOBJ_API LuaMemPool
{
public:
	LuaMemPool(){
	}

	~LuaMemPool(){
	}

	inline void* malloc(size_t bytes){
		return ::malloc(bytes);
	}

	inline void free(void* mem){
		return ::free(mem);
	}

	inline void* realloc(void* mem, size_t newsize){
		return ::realloc(mem, newsize);
	}

	inline void* calloc(size_t n_elements, size_t elem_size){
		return ::calloc(n_elements, elem_size);
	}
};

#endif


